class Solution {
    public:
        int maxSubarraySumCircular(vector<int>& nums) {
            int n = nums.size();
            vector<int> f(n+1); //以i位结尾的最大子数组和
            vector<int> g(n+1); //以i为结尾的最小子数组和
            int sum = 0;
            for(auto& x:nums) sum+=x;
            //f[0]=g[0]=nums[0];
            int ret = nums[0];
            for(int i = 1 ; i < n+1 ; i++){
                f[i] = max(f[i-1]+nums[i-1],nums[i-1]);
                g[i] = min(g[i-1]+nums[i-1],nums[i-1]);
                
                if(g[i] != sum) ret = max(ret,max(f[i],sum-g[i-1]));
                else ret = max(ret,f[i]);
                //cout<<ret<<endl;
                
            }
            return ret;
        }
    };